package guiScreens;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;

import javax.swing.*;

import creature.Attack;
import creature.Creature;

import battle.Battle;

//TODO:  Separate battleGUI from battle match... this could be a lot of work ... happening.
public class battleScreen extends JFrame implements ActionListener
{
	//Private Data Members//
	//Buttons//
	private JButton attack1ButtonL;
	private JButton attack2ButtonL;
	private JButton attack3ButtonL;
	private JButton attack4ButtonL;
    private JButton attack1ButtonR;
    private JButton attack2ButtonR;
    private JButton attack3ButtonR;
    private JButton attack4ButtonR;
    
    //JLables//
    private JLabel lHPTag;
    private JLabel statL; //Not used
    private JLabel rHPTag;
    private JLabel statR; //Not used
    private JLabel attack1LabelL;
    private JLabel attack2LabelL;
    private JLabel attack3LabelL;
    private JLabel attack4LabelL;
    private JLabel attack1LabelR;
    private JLabel attack2LabelR;
    private JLabel attack3LabelR;
    private JLabel attack4LabelR;
    
    //Creatures//
    Creature leftCreature;
    Creature rightCreature;
    Creature active;
    Creature waiting;
    boolean activeCreature; //True is left false is right
    
    //Constructor: builds screen//
	public battleScreen() throws FileNotFoundException
	{
		//Set up creatures//
		buildBattle();
		
		//Set up panel//
		JPanel left = initLeft();
		JPanel right = initRight();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout());
		panel.add(left);
		panel.add(right);
		
		//Set frame settings//
		this.setSize(800,500);
		this.setLocationRelativeTo(null);
		this.add(panel);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		//Disable applicable buttons//
		disablePlayer();
	}
	
	//Sets up the left side of battle screens//
	private JPanel initLeft()
	{
		//Set up left panel//
		JPanel left = new JPanel();	
		left.setLayout(new GridLayout(2,0));
		
		//Add Image//
		Icon leftImage = new ImageIcon("images/Charizard.png");
		JLabel leftBattleScreen = new JLabel(leftImage);
		left.add(leftBattleScreen);
		
		//Set up lower left//
		JPanel lowerLeft = new JPanel();
		lowerLeft.setLayout(new GridLayout(0,2,10,10));
		
		//Get attacks//
		Attack a1 = leftCreature.getAttack(1);	//TODO: This will eventaully not work as imagined, I want attack no to
		Attack a2 = leftCreature.getAttack(2);  // correspond to a db number so builing attack buttons will change
		Attack a3 = leftCreature.getAttack(3);	//Maybe LL of attacks?
		Attack a4 = leftCreature.getAttack(4);
		
		//JLabels//
		lHPTag = new JLabel("HP:          " + leftCreature.getHP());
		statL = new JLabel("STATS TAG");
		attack1LabelL = new JLabel("Damage: " + a1.getDamage());
		attack2LabelL = new JLabel("Damage: " + a2.getDamage());
		attack3LabelL = new JLabel("Damage: " + a3.getDamage());
		attack4LabelL = new JLabel("Damage: " + a4.getDamage());
		
		//Buttons//
		attack1ButtonL = new JButton(a1.getName());
		attack1ButtonL.addActionListener(this);
		attack2ButtonL = new JButton(a2.getName());
		attack2ButtonL.addActionListener(this);
		attack3ButtonL = new JButton(a3.getName());
		attack3ButtonL.addActionListener(this);
		attack4ButtonL = new JButton(a4.getName());
		attack4ButtonL.addActionListener(this);
		
		//Add to panel//
		lowerLeft.add(lHPTag);
		lowerLeft.add(statL);
		lowerLeft.add(attack1ButtonL);
		lowerLeft.add(attack1LabelL);
		lowerLeft.add(attack2ButtonL);
		lowerLeft.add(attack2LabelL);
		lowerLeft.add(attack3ButtonL);
		lowerLeft.add(attack3LabelL);
		lowerLeft.add(attack4ButtonL);
		lowerLeft.add(attack4LabelL);
	
		left.add(lowerLeft);
		return left;
	}
	
	//Sets up the right side of battle screens//
	private JPanel initRight()
	{
		//Set up right panel//
		JPanel right = new JPanel();		
		right.setLayout(new GridLayout(2,0));
		
		Icon rightImage = new ImageIcon("images/Blastoise.png");
		JLabel rightBattleScreen = new JLabel(rightImage);
		right.add(rightBattleScreen);
		
		//Set up lower right//
		JPanel lowerRight = new JPanel();
		lowerRight.setLayout(new GridLayout(0,2,10,10));
		
		//Attacks//
		Attack a1 = rightCreature.getAttack(1);	//TODO: This will eventaully not work as imagined, I want attack no to
		Attack a2 = rightCreature.getAttack(2);  // correspond to a db number so builing attack buttons will change
		Attack a3 = rightCreature.getAttack(3);	//Maybe LL of attacks?
		Attack a4 = rightCreature.getAttack(4);
		
		//JLabels//
		rHPTag = new JLabel("HP:          " + rightCreature.getHP());
		statR = new JLabel("STATS TAG");
		attack1LabelR = new JLabel("Damage: " + a1.getDamage());
		attack2LabelR = new JLabel("Damage: " + a2.getDamage());		
		attack3LabelR = new JLabel("Damage: " + a3.getDamage());
		attack4LabelR = new JLabel("Damage: " + a4.getDamage());
		
		//Buttons//
		attack1ButtonR = new JButton(a1.getName());
		attack1ButtonR.addActionListener(this);
		attack2ButtonR = new JButton(a2.getName());
		attack2ButtonR.addActionListener(this);
		attack3ButtonR = new JButton(a3.getName());
		attack3ButtonR.addActionListener(this);
		attack4ButtonR = new JButton(a4.getName());
		attack4ButtonR.addActionListener(this);
		
		//Add to panel//
		lowerRight.add(rHPTag);
		lowerRight.add(statR);
		lowerRight.add(attack1ButtonR);
		lowerRight.add(attack1LabelR);
		lowerRight.add(attack2ButtonR);
		lowerRight.add(attack2LabelR);
		lowerRight.add(attack3ButtonR);
		lowerRight.add(attack3LabelR);
		lowerRight.add(attack4ButtonR);
		lowerRight.add(attack4LabelR);
	
		right.add(lowerRight);
		return right;
	}

	//Initializes stuff needed for battle//
	public void buildBattle() throws FileNotFoundException
	{
		//Initialize creatures//
		leftCreature = new Creature("Creature1");
		rightCreature = new Creature("Creature2");
		active = leftCreature;
		waiting = rightCreature;
		activeCreature = true;
	}
	
	//Initiates Attack//
	private void attack(Attack a)
	{
		//Establish damage//
		waiting.receiveAttack(a);
		
		//Check for match end//
		checkForWinner();
		
		//Swap//
		switchTurn();
	}
	
	//Swaps who is active creature and who is waiting creature//
	private void switchTurn()
	{
		Creature holder = active;
		active = waiting;
		waiting = holder;
		activeCreature = !activeCreature;
		disablePlayer();
	}
	
	//Disable opposite controls//
	private void disablePlayer()
	{
		attack1ButtonR.setEnabled(!activeCreature);
		attack2ButtonR.setEnabled(!activeCreature);
		attack3ButtonR.setEnabled(!activeCreature);
		attack4ButtonR.setEnabled(!activeCreature);
		attack1ButtonL.setEnabled(activeCreature);
		attack2ButtonL.setEnabled(activeCreature);
		attack3ButtonL.setEnabled(activeCreature);
		attack4ButtonL.setEnabled(activeCreature);
	}
	
	//Determines if attack wins match//
	private void checkForWinner()
	{
		int hp = waiting.getHP();
		if ( hp<=0 )
		{
			JOptionPane.showMessageDialog(null, active.getName() + " player is the winner.");
			this.dispose();
		}
	}

	//Listen for button clicked//
	public void actionPerformed(ActionEvent e)
	{	
		//Set stuff up//
		JButton x = new JButton();
		x = (JButton) e.getSource();
		
		//Determine Attack used//
		Attack attack = active.getAttack(x.getText()); 
		System.out.println(attack);
		
		//Attack//
		attack(attack);
		
		//Adjust HP//
		rHPTag.setText("HP: " + rightCreature.getHP());
		lHPTag.setText("HP: " + leftCreature.getHP());
	}
}